﻿using DNFImagePacks2Manager.Helpers;
using Microsoft.UI.Xaml.Controls;
using System;
using System.Collections.Generic;
using System.IO;

namespace DNFImagePacks2Manager.Models
{
    public class BKItemWhole(string folderName) : BKItemBase(folderName, "套装")
    {
        public string BKFolderName { get; set; } = folderName;
        public string BKFolder => Path.Combine(BKHelper.GetWholeFolder(), BKFolderName);

        public override void Apply(Page page)
        {
            if (!LocalStorageHelper.IsSetDNFPath())
            {
                DialogHelper.ShowErrorMessage(page, "应用失败: 未设置DNF路径");
                return;
            }
            string bkFolder = BKFolder;
            if (!Directory.Exists(bkFolder)) {
                DialogHelper.ShowErrorMessage(page, "应用失败: 目录不存在");
                return;
            }
            string bkPath = LocalStorageHelper.GetBKOfficialPath();
            string[] fullPaths = Directory.GetFiles(bkFolder, "*.*", SearchOption.AllDirectories);
            int success = 0;
            List<string> fails = [];
            int totalFile = fullPaths.Length;
            foreach (string path in fullPaths)
            {
                string relativePath = Path.GetRelativePath(bkFolder, path);
                string targetPath = Path.Combine(bkPath, relativePath);
                try
                {
                    if (File.Exists(targetPath)) {
                        File.Delete(targetPath);
                    }
                    File.Copy(path, targetPath);
                    success++;
                }
                catch (Exception)
                {
                    fails.Add(relativePath);
                }
            }
            if (fails.Count == 0) {
                DialogHelper.ShowSuccessMessage(page, $"成功，应用 {success} 个文件");
            }
            else
            {
                ListView listView = new()
                {
                    ItemsSource = fails
                };

                DialogHelper.ShowNormalDialog(page, $"{fails.Count} 个文件应用失败", listView);
            }
        }

        public override bool Delete(Page page)
        {
            try
            {
                string folder = BKFolder;
                if (Directory.Exists(folder))
                {
                    Directory.Delete(folder, true);
                }
                DialogHelper.ShowSuccessMessage(page, "删除成功");
                return true;
            }
            catch(Exception ex)
            {
                DialogHelper.ShowErrorMessage(page, $"删除失败: {ex.Message}");
                return false;
            }
        }

        public override bool Rename(Page page, string name)
        {
            try
            {
                string folder = BKFolder;
                string newFolder = Path.Combine(BKHelper.GetWholeFolder(), name);
                if (Directory.Exists(folder))
                {
                    if (Directory.Exists(newFolder)) {
                        DialogHelper.ShowErrorMessage(page, "重命名失败: 目标文件夹已存在");
                    }
                    else
                    {
                        Directory.Move(folder, newFolder);
                        BKFolderName = name;
                        Name = name;
                        DialogHelper.ShowSuccessMessage(page, "重命名完成");
                        return true;
                    }
                }else
                {
                    DialogHelper.ShowErrorMessage(page, "重命名失败: 找不到目录");
                }
            }
            catch (Exception ex)
            {
                DialogHelper.ShowErrorMessage(page, $"重命名失败: {ex.Message}");
            }
            return false;
        }

        public override void Preview(Page page)
        {
            try
            {
                string bkFolder = BKFolder;
                if (!Directory.Exists(bkFolder))
                {
                    DialogHelper.ShowErrorMessage(page, "预览失败: 目录不存在");
                    return;
                }
                var categories = BKPathHelper.CategoryDirectory;
                string bkPath = LocalStorageHelper.GetBKOfficialPath();
                string[] fullPaths = Directory.GetFiles(bkFolder, "*.*", SearchOption.AllDirectories);
                List<string> nameList = [];
                foreach (string path in fullPaths)
                {
                    string relativePath = Path.GetRelativePath(bkFolder, path);
                    string name = categories.GetValueOrDefault(relativePath, relativePath);
                    nameList.Add(name);
                }

                ListView listView = new()
                {
                    ItemsSource = nameList
                };

                DialogHelper.ShowNormalDialog(page, $"{Name} ({nameList.Count})", listView);
            }
            catch (Exception ex)
            {
                DialogHelper.ShowErrorMessage(page, $"预览失败: {ex.Message}");
            }
        }

        public override void Edit(Page page)
        {
            string bkFolder = BKFolder;
            if (!Directory.Exists(bkFolder))
            {
                DialogHelper.ShowErrorMessage(page, "编辑失败: 目录不存在");
                return;
            }
            ConsoleHelper.OpenFolderInExplorer(bkFolder);
        }

        public override void Export(Page page)
        {
            string bkFolder = BKFolder;
            if (!Directory.Exists(bkFolder))
            {
                DialogHelper.ShowErrorMessage(page, "导出失败: 目录不存在");
                return;
            }
            string rootFolder = "Video";
            string[] fullPaths = Directory.GetFiles(bkFolder, "*.*", SearchOption.AllDirectories);
            Dictionary<string, string> compressFiles = [];
            foreach (string path in fullPaths)
            {
                string relativePath = Path.GetRelativePath(bkFolder, path);
                string targetPath = Path.Combine(rootFolder, relativePath);
                compressFiles.Add(targetPath, path);
            }

            DialogHelper.ShowSavePickerAndExportZip(page, Name, compressFiles);
        }
    }
}
